package com.offcn.servlet;

import com.offcn.entity.Book;
import com.offcn.service.BookService;
import com.offcn.service.Impl.BookServiceImpl;
import com.offcn.utils.BaseServlet;
import com.offcn.utils.Page;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "BookServlet", urlPatterns = {"/BookServlet"})
public class BookServlet extends BaseServlet {
    //创建bookservice实现类对象
    private BookService bookService = new BookServiceImpl();

    //首页书籍分页展示功能
    protected void getBookList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取manage.jsp中a标签夹带传来的pageno
        String pageno = request.getParameter("pageno");
        Page<Book> page = bookService.getBookList(pageno);
        request.setAttribute("page",page);
        //转发至book_manager页面
        request.getRequestDispatcher("/book/book_manager.jsp").forward(request,response);
    }



    //进入编辑页面的配置，添加和修改都能进,如果是修改，传来的有Id 值，可以直接根据id去寻找book展示出来，如果是添加，就没有id,展示不出来
        protected void getBookById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //首先获取book.id,pageNo
            String pageNo = request.getParameter("pageNo");
            String bookId = request.getParameter("id");
            Book book = bookService.getBookByID(bookId);
            //存放在request里
            request.setAttribute("book",book);
            request.setAttribute("pageNo",pageNo);
            request.getRequestDispatcher("/book/book_edit.jsp").forward(request,response);

    }


    //执行相应的功能
    protected void addOrmodify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //首先获取book.id,区分是添加还是修改
        String bookId = request.getParameter("id");
        String pageNo = request.getParameter("pageNo");
        //获取其他数据  在service层进行数据转换，并装入集合
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String price = request.getParameter("price");
        String sales = request.getParameter("sales");
        String stock = request.getParameter("stock");


        if(bookId!=null && !bookId.equals("")){
            try {
                //是修改,调用修改方法
                bookService.modifyBook(bookId,title,author,price,sales,stock);
                //提示信息最好是放在session里，显示立即删除
                request.getSession().setAttribute("msg","【"+title+"】修改成功");
                response.sendRedirect(request.getContextPath()+"/BookServlet?way=getBookList&pageno="+pageNo);
            } catch (Exception e) {
                request.getSession().setAttribute("errorMsg","修改异常，请检查数据类型");
                response.sendRedirect(request.getContextPath()+"BookServlet?way=getBookById&id="+bookId+"&pageNo=}"+pageNo);
            }

        }else{
            try {
                //是添加，调用添加方法
                bookService.addBook(title,author,price,sales,stock);
                request.getSession().setAttribute("msg","【"+title+"】添加成功");
                response.sendRedirect(request.getContextPath()+"/BookServlet?way=getBookList&pageno="+pageNo);
            } catch (Exception e) {
                request.getSession().setAttribute("errorMsg","添加异常");
                response.sendRedirect(request.getContextPath()+"BookServlet?way=getBookById&id="+bookId+"&pageNo=}"+pageNo);
            }
        }

    }

    //删除book
    protected void deleteBookById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取id
        String pageNo = request.getParameter("pageNo");
        String bookId = request.getParameter("id");
        String title = request.getParameter("title");
        System.out.println(title);
        try {
            bookService.deleteBookById(bookId);
            //删除成功返回book_manager页面,并提示信息
            request.getSession().setAttribute("msg",title+"删除成功");
            response.sendRedirect(request.getContextPath()+"/BookServlet?way=getBookList&pageno="+pageNo);
        } catch (Exception e) {
            request.getSession().setAttribute("msg",title+"删除异常");
            response.sendRedirect(request.getContextPath()+"/BookServlet?way=getBookList&pageno="+pageNo);
        }


    }
}
